package com.smartcruise.report;

import android.media.MediaRecorder;

import com.smartcruise.util.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by FengChaoQun
 * on 2018/1/24 0024
 * 录音管理
 */

public class AudioRecordManager {

    private MediaRecorder mRecorder;
    private String audioPath = FileUtils.getNewAudioPath();
    private boolean isRecording; //只有开始录音了 才能结束

    public void startRecord() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setOutputFile(audioPath);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mRecorder.start();
        isRecording = true;
    }

    public void endRecord() {
        if (mRecorder == null || !isRecording) {
            return;
        }
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
        isRecording = false;
    }

    /**
     * description:销毁当前录音
     */

    public void dropAudio() {
        try {
            File file = new File(audioPath);
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * description:销毁
     */

    public void onDestroy() {
        endRecord();
    }

    public String getAudioPath() {
        return audioPath;
    }
}
